#include "main.h"

/* 
--------------------------------------------------------------------------------
	函数功能：磁盘内存设置
--------------------------------------------------------------------------------
*/
int disk_set_share(int sockfd, char *tagid, char *msg)
{
	int ret = 0, flag = 1;
	unsigned int free = 0;
	char cmd[256];
	char value[128];
	char share[16];
	char data[1024];
	cJSON *pRow = NULL, *pRoot = NULL, *pValue = NULL;

	pRow = cJSON_CreateObject();

	pRoot = cJSON_Parse(msg);
	if (pRoot == NULL)
	{
		cJSON_AddNumberToObject(pRow, "code", 1);
		cJSON_AddStringToObject(pRow, "msg", "JSON解析失败!");
	}
	else
	{
		memset(share, 0, sizeof(share));
		pValue = cJSON_GetObjectItem(pRoot, "share");
		if (pValue != NULL && pValue->type == cJSON_Number)
		{
			snprintf(share, sizeof(share), "%d", pValue->valueint);

			memset(value, 0, sizeof(value));
			ret = get_value("appctrl.@appctrl[0].share", &value);
			if (ret == 1 && strlen(value))
				free = atol(value);

			if (pValue->valueint > free && strlen(DiskPath))
			{
				free = pValue->valueint - free;

				memset(cmd, 0, sizeof(cmd));
				memset(value, 0, sizeof(value));
				snprintf(cmd, sizeof(cmd), "/usr/sbin/diskcheck.sh %s %d", DiskPath, free);
				shell_cmd(cmd, value);
				flag = atoi(value);
			}

			if (!strlen(DiskPath))
				flag = 0;

			if (flag)
			{
				set_value("appctrl.@appctrl[0].share", share);
				cJSON_AddNumberToObject(pRow, "code", 0);
				cJSON_AddStringToObject(pRow, "msg", "Success");
			}
			else
			{
				cJSON_AddNumberToObject(pRow, "code", 1);
				cJSON_AddStringToObject(pRow, "msg", "磁盘内存不足");
			}
		}
		else
		{
			cJSON_AddNumberToObject(pRow, "code", 1);
			cJSON_AddStringToObject(pRow, "msg", "磁盘内存设置,参数错误");
		}
	}

	pRoot = cJSON_CreateObject();
	cJSON_AddStringToObject(pRoot, "type", "COMMON_SUCCESS");
	cJSON_AddStringToObject(pRoot, "replyType", "DEVICE_DISK_SET");
	cJSON_AddStringToObject(pRoot, "clientId", RouteID);
	cJSON_AddStringToObject(pRoot, "tagClientId", tagid);
	cJSON_AddStringToObject(pRoot, "content", cJSON_Print(pRow));

	char *buffer = cJSON_Print(pRoot);
	buffer[strlen(buffer)] = '\0';

	if (pRow != NULL)
		cJSON_Delete(pRow);

	if (pRoot != NULL)
		cJSON_Delete(pRoot);

	ret = send_msg(sockfd, buffer, strlen(buffer));
	free(buffer);
	buffer = NULL;

	return ret;
}

/* 
--------------------------------------------------------------------------------
	函数功能：磁盘内存获取
--------------------------------------------------------------------------------
*/
int disk_get_share(int sockfd, char *tagid)
{
	int ret = 0;
	char value[128];
	char data[1024];
	cJSON *pRoot = NULL, *pRow = NULL;

	pRow = cJSON_CreateObject();

	//share
	memset(value, 0, sizeof(value));
	ret = get_value("appctrl.@appctrl[0].share", &value);
	if (ret == 1 && strlen(value))
		cJSON_AddNumberToObject(pRow, "share", atol(value));
	else
		cJSON_AddNumberToObject(pRow, "share", 0);

	//total
	memset(value, 0, sizeof(value));
	shell_cmd("df -k | grep '/dev/mmcblk' | awk '{print $2}'", value);
	if (strlen(value))
		cJSON_AddNumberToObject(pRow, "total", atol(value));
	else
	{
		cJSON_AddNumberToObject(pRow, "total", 0);
		cJSON_AddNumberToObject(pRow, "share", 0);
	}

	pRoot = cJSON_CreateObject();
	cJSON_AddStringToObject(pRoot, "type", "DEVICE_DISKGET_REP");
	cJSON_AddStringToObject(pRoot, "replyType", "DEVICE_DISK_REQ");
	cJSON_AddStringToObject(pRoot, "clientId", RouteID);
	cJSON_AddStringToObject(pRoot, "tagClientId", tagid);
	cJSON_AddStringToObject(pRoot, "content", cJSON_Print(pRow));

	char *buffer = cJSON_Print(pRoot);
	buffer[strlen(buffer)] = '\0';

	if (pRow != NULL)
		cJSON_Delete(pRow);

	if (pRoot != NULL)
		cJSON_Delete(pRoot);

	ret = send_msg(sockfd, buffer, strlen(buffer));
	free(buffer);
	buffer = NULL;

	return ret;
}

/* 
--------------------------------------------------------------------------------
	函数功能：获取目录大小
--------------------------------------------------------------------------------
*/
int disk_size_file(int sockfd, char *tagid)
{
	int ret = 0;
	char cmd[256];
	char value[128];
	char data[1024];
	cJSON *pRoot = NULL, *pRow = NULL;

	pRow = cJSON_CreateObject();

	//photo
	memset(cmd, 0, sizeof(cmd));
	memset(value, 0, sizeof(value));
	snprintf(cmd, sizeof(cmd), "du -k %s/photo 2>/dev/null | awk '{print $1}'", DiskPath);
	shell_cmd(cmd, value);
	if (strlen(value))
		cJSON_AddNumberToObject(pRow, "photo", atol(value));
	else
		cJSON_AddNumberToObject(pRow, "photo", 0);

	//movie
	memset(cmd, 0, sizeof(cmd));
	memset(value, 0, sizeof(value));
	snprintf(cmd, sizeof(cmd), "du -k %s/movie 2>/dev/null | awk '{print $1}'", DiskPath);
	shell_cmd(cmd, value);
	if (strlen(value))
		cJSON_AddNumberToObject(pRow, "movie", atol(value));
	else
		cJSON_AddNumberToObject(pRow, "movie", 0);

	//music
	memset(cmd, 0, sizeof(cmd));
	memset(value, 0, sizeof(value));
	snprintf(cmd, sizeof(cmd), "du -k %s/music 2>/dev/null | awk '{print $1}'", DiskPath);
	shell_cmd(cmd, value);
	if (strlen(value))
		cJSON_AddNumberToObject(pRow, "music", atol(value));
	else
		cJSON_AddNumberToObject(pRow, "music", 0);

	pRoot = cJSON_CreateObject();
	cJSON_AddStringToObject(pRoot, "type", "DEVICE_FILE_STORAGE_REPLY");
	cJSON_AddStringToObject(pRoot, "replyType", "DEVICE_FILE_STORAGE_REQ");
	cJSON_AddStringToObject(pRoot, "clientId", RouteID);
	cJSON_AddStringToObject(pRoot, "tagClientId", tagid);
	cJSON_AddStringToObject(pRoot, "content", cJSON_Print(pRow));

	char *buffer = cJSON_Print(pRoot);
	buffer[strlen(buffer)] = '\0';

	if (pRow != NULL)
		cJSON_Delete(pRow);

	if (pRoot != NULL)
		cJSON_Delete(pRoot);

	ret = send_msg(sockfd, buffer, strlen(buffer));
	free(buffer);
	buffer = NULL;

	return ret;
}

/* 
--------------------------------------------------------------------------------
	函数功能：获取文件子项目
--------------------------------------------------------------------------------
*/
int disk_list_file(int sockfd, char *tagid, char *msg)
{
	int ret = 0, flag = 1;
	DIR *dir;
	unsigned long size = 0;
	struct dirent *ptr;
	struct stat file;
	char path[128];
	char filePath[12];
	char data[BUFF_SIZE];
	cJSON *pRow = NULL, *pRoot = NULL, *pValue = NULL;

	pRow = cJSON_CreateArray();

	pRoot = cJSON_Parse(msg);
	if (pRoot != NULL)
	{
		//文件类型
		memset(filePath, 0, sizeof(filePath));
		pValue = cJSON_GetObjectItem(pRoot, "fileType");
		if (pValue != NULL && pValue->type == cJSON_Number)
		{
			if (pValue->valueint == 0)
				memcpy(filePath, "movie", 5);
			else if (pValue->valueint == 1)
				memcpy(filePath, "photo", 5);
			else if (pValue->valueint == 2)
				memcpy(filePath, "music", 5);
		}

		if (strlen(DiskPath) && strlen(filePath))
		{
			memset(path, 0, sizeof(path));
			snprintf(path, sizeof(path), "%s/%s", DiskPath, filePath);

			if ((dir = opendir(path)) != NULL)
			{
				while ((ptr = readdir(dir)) != NULL)
				{
					memset(path, 0, sizeof(path));
					snprintf(path, sizeof(path), "%s/%s/%s", DiskPath, filePath, ptr->d_name);

					if (strncmp(ptr->d_name, ".", 1) == 0)
						continue;
					if (stat(path, &file) >= 0)
					{
						if (file.st_size > 1024)
							size = file.st_size / 1024;
						else
							size = 1;

						cJSON_AddItemToArray(pRow, pRoot = cJSON_CreateObject());
						cJSON_AddStringToObject(pRoot, "fileName", ptr->d_name);
						cJSON_AddNumberToObject(pRoot, "uploadTime", file.st_mtime);
						cJSON_AddNumberToObject(pRoot, "fileSize", size);
						cJSON_AddStringToObject(pRoot, "filePath", path);
					}
				}
				closedir(dir);
			}
		}
	}

	pRoot = cJSON_CreateObject();
	cJSON_AddStringToObject(pRoot, "type", "DEVICE_FILE_STORAGEITEM_REPLY");
	cJSON_AddStringToObject(pRoot, "replyType", "DEVICE_FILE_STORAGEITEM_REQ");
	cJSON_AddStringToObject(pRoot, "clientId", RouteID);
	cJSON_AddStringToObject(pRoot, "tagClientId", tagid);
	cJSON_AddStringToObject(pRoot, "content", cJSON_Print(pRow));

	char *buffer = cJSON_Print(pRoot);
	buffer[strlen(buffer)] = '\0';

	if (pRow != NULL)
		cJSON_Delete(pRow);

	if (pRoot != NULL)
		cJSON_Delete(pRoot);

	ret = send_msg(sockfd, buffer, strlen(buffer));
	free(buffer);
	buffer = NULL;

	return ret;
}

/* 
--------------------------------------------------------------------------------
	函数功能：文件重命名
--------------------------------------------------------------------------------
*/
int disk_rename_file(int sockfd, char *tagid, char *msg)
{
	int ret = 0;
	char cmd[1024];
	char oldName[256];
	char newName[256];
	char data[1024];
	cJSON *pRow = NULL, *pRoot = NULL, *pValue = NULL;

	pRow = cJSON_CreateObject();

	pRoot = cJSON_Parse(msg);
	if (pRoot == NULL)
	{
		cJSON_AddNumberToObject(pRow, "code", 1);
		cJSON_AddStringToObject(pRow, "msg", "JSON解析失败!");
	}
	else
	{
		//旧文件名称
		memset(oldName, 0, sizeof(oldName));
		pValue = cJSON_GetObjectItem(pRoot, "oldName");
		if (pValue != NULL && pValue->type == cJSON_String)
			memcpy(oldName, pValue->valuestring, sizeof(oldName));

		//新文件名称
		memset(newName, 0, sizeof(newName));
		pValue = cJSON_GetObjectItem(pRoot, "newName");
		if (pValue != NULL && pValue->type == cJSON_String)
			memcpy(newName, pValue->valuestring, sizeof(newName));

		if (strlen(oldName) && strlen(newName))
		{
			struct stat file;
			if (stat(oldName, &file) < 0)
			{
				cJSON_AddNumberToObject(pRow, "code", 1);
				cJSON_AddStringToObject(pRow, "msg", "文件不存在");
			}
			else
			{
				memset(cmd, 0, sizeof(cmd));
				snprintf(cmd, sizeof(cmd), "mv %s %s", oldName, newName);
				system(cmd);

				cJSON_AddNumberToObject(pRow, "code", 0);
				cJSON_AddStringToObject(pRow, "msg", "Success");
			}
		}
		else
		{
			cJSON_AddNumberToObject(pRow, "code", 1);
			cJSON_AddStringToObject(pRow, "msg", "参数错误");
		}
	}

	pRoot = cJSON_CreateObject();
	cJSON_AddStringToObject(pRoot, "type", "COMMON_SUCCESS");
	cJSON_AddStringToObject(pRoot, "replyType", "DEVICE_STORAGEITEM_RENAME_REQ");
	cJSON_AddStringToObject(pRoot, "clientId", RouteID);
	cJSON_AddStringToObject(pRoot, "tagClientId", tagid);
	cJSON_AddStringToObject(pRoot, "content", cJSON_Print(pRow));

	char *buffer = cJSON_Print(pRoot);
	buffer[strlen(buffer)] = '\0';

	if (pRow != NULL)
		cJSON_Delete(pRow);

	if (pRoot != NULL)
		cJSON_Delete(pRoot);

	ret = send_msg(sockfd, buffer, strlen(buffer));
	free(buffer);
	buffer = NULL;

	return ret;
}

/* 
--------------------------------------------------------------------------------
	函数功能：文件删除
--------------------------------------------------------------------------------
*/
int disk_delete_file(int sockfd, char *tagid, char *msg)
{
	int ret = 0;
	char cmd[1024];
	char filePath[256];
	char data[1024];
	cJSON *pRow = NULL, *pRoot = NULL, *pValue = NULL;

	pRow = cJSON_CreateObject();

	pRoot = cJSON_Parse(msg);
	if (pRoot == NULL)
	{
		cJSON_AddNumberToObject(pRow, "code", 1);
		cJSON_AddStringToObject(pRow, "msg", "JSON解析失败!");
	}
	else
	{
		//文件名称
		memset(filePath, 0, sizeof(filePath));
		pValue = cJSON_GetObjectItem(pRoot, "filePath");
		if (pValue != NULL && pValue->type == cJSON_String)
		{
			memcpy(filePath, pValue->valuestring, sizeof(filePath));

			struct stat file;
			if (stat(filePath, &file) < 0)
			{
				cJSON_AddNumberToObject(pRow, "code", 1);
				cJSON_AddStringToObject(pRow, "msg", "文件不存在");
			}
			else
			{
				memset(cmd, 0, sizeof(cmd));
				snprintf(cmd, sizeof(cmd), "rm -f %s", filePath);
				system(cmd);

				cJSON_AddNumberToObject(pRow, "code", 0);
				cJSON_AddStringToObject(pRow, "msg", "Success");
			}
		}
		else
		{
			cJSON_AddNumberToObject(pRow, "code", 1);
			cJSON_AddStringToObject(pRow, "msg", "参数错误");
		}
	}

	pRoot = cJSON_CreateObject();
	cJSON_AddStringToObject(pRoot, "type", "COMMON_SUCCESS");
	cJSON_AddStringToObject(pRoot, "replyType", "DEVICE_STORAGEITEM_RENAME_REQ");
	cJSON_AddStringToObject(pRoot, "clientId", RouteID);
	cJSON_AddStringToObject(pRoot, "tagClientId", tagid);
	cJSON_AddStringToObject(pRoot, "content", cJSON_Print(pRow));

	char *buffer = cJSON_Print(pRoot);
	buffer[strlen(buffer)] = '\0';

	if (pRow != NULL)
		cJSON_Delete(pRow);

	if (pRoot != NULL)
		cJSON_Delete(pRoot);

	ret = send_msg(sockfd, buffer, strlen(buffer));
	free(buffer);
	buffer = NULL;

	return ret;
}

/* 
--------------------------------------------------------------------------------
	函数功能：写文件
--------------------------------------------------------------------------------
*/
int write_bytes(char *file, int index, char *data, int len)
{
	int ret = 0;
	FILE *fp;

	if ((fp = fopen(file, "a")) == NULL)
	{
		printf("open file %s error, fp == NULL !\n", file);
		return -1;
	}

	ret = fseek(fp, index, SEEK_SET);
	if (ret == -1)
	{
		printf("seek file %s error! \n", file);
		return -1;
	}

	fwrite(data, len, 1, fp);

	fclose(fp);

	return 0;
}

/* 
--------------------------------------------------------------------------------
	函数功能：文件上传,路由器写入文件
--------------------------------------------------------------------------------
*/
int disk_write_file(int sockfd, char *tagid, char *msg)
{
	int ret = 0, flag = 1;
	char file[256];
	char fileId[128];
	char fileName[128];
	char filePath[12];
	char sizeUnit[12];
	unsigned long size = 0;
	unsigned long totalSize = 0;
	unsigned long currentIndex = 0;
	char data[1024];
	cJSON *pRow = NULL, *pRoot = NULL, *pValue = NULL;

	pRow = cJSON_CreateObject();

	pRoot = cJSON_Parse(msg);
	if (pRoot == NULL)
	{
		cJSON_AddNumberToObject(pRow, "code", 1);
		cJSON_AddStringToObject(pRow, "msg", "JSON解析失败!");
	}
	else
	{
		//文件ID
		memset(fileId, 0, sizeof(fileId));
		pValue = cJSON_GetObjectItem(pRoot, "fileId");
		if (pValue != NULL && pValue->type == cJSON_String)
			memcpy(fileId, pValue->valuestring, sizeof(fileId));

		//文件名称
		memset(fileName, 0, sizeof(fileName));
		pValue = cJSON_GetObjectItem(pRoot, "fileName");
		if (pValue != NULL && pValue->type == cJSON_String)
			memcpy(fileName, pValue->valuestring, sizeof(fileName));

		//文件路径
		memset(filePath, 0, sizeof(filePath));
		pValue = cJSON_GetObjectItem(pRoot, "filePath");
		if (pValue != NULL && pValue->type == cJSON_String)
			memcpy(filePath, pValue->valuestring, sizeof(filePath));

		//文件总长度
		pValue = cJSON_GetObjectItem(pRoot, "totalSize");
		if (pValue != NULL && pValue->type == cJSON_Number)
			totalSize = pValue->valueint;

		//传输单位长度
		memset(sizeUnit, 0, sizeof(sizeUnit));
		pValue = cJSON_GetObjectItem(pRoot, "sizeUnit");
		if (pValue != NULL && pValue->type == cJSON_String)
			memcpy(sizeUnit, pValue->valuestring, sizeof(sizeUnit));

		//字节索引
		pValue = cJSON_GetObjectItem(pRoot, "currentIndex");
		if (pValue != NULL && pValue->type == cJSON_Number)
			currentIndex = pValue->valueint;

		//切片长度
		pValue = cJSON_GetObjectItem(pRoot, "size");
		if (pValue != NULL && pValue->type == cJSON_Number)
			size = pValue->valueint;

		//切片数组
		char bytes[size + 4];
		memset(bytes, 0, sizeof(bytes));
		pValue = cJSON_GetObjectItem(pRoot, "bytes");
		if (pValue != NULL && pValue->type == cJSON_String)
			memcpy(bytes, pValue->valuestring, sizeof(bytes));

		//文件类型
		memset(filePath, 0, sizeof(filePath));
		pValue = cJSON_GetObjectItem(pRoot, "fileType");
		if (pValue != NULL && pValue->type == cJSON_Number)
		{
			if (pValue->valueint == 0)
				memcpy(filePath, "movie", 5);
			else if (pValue->valueint == 1)
				memcpy(filePath, "photo", 5);
			else if (pValue->valueint == 2)
				memcpy(filePath, "music", 5);
		}

		if (currentIndex == 0)
		{
			char value[4];
			char cmd[128];

			memset(cmd, 0, sizeof(cmd));
			memset(value, 0, sizeof(value));

			snprintf(cmd, sizeof(cmd), "/usr/sbin/diskcheck.sh %s %d", DiskPath, totalSize);
			shell_cmd(cmd, value);
			flag = atoi(value);
		}

		if (flag)
		{
			memset(file, 0, sizeof(file));
			//snprintf(file, sizeof(file), "%s/%s/%s_%s", DiskPath, filePath, fileName, fileId);
			snprintf(file, sizeof(file), "%s/%s/%s", DiskPath, filePath, fileName);

			write_bytes(file, currentIndex, bytes, size);

			cJSON_AddNumberToObject(pRow, "code", 0);
			cJSON_AddStringToObject(pRow, "msg", "Success!");
		}
		else
		{
			cJSON_AddNumberToObject(pRow, "code", 1);
			cJSON_AddStringToObject(pRow, "msg", "空间内存不足!");
		}
	}

	pRoot = cJSON_CreateObject();
	cJSON_AddStringToObject(pRoot, "type", "COMMON_SUCCESS");
	cJSON_AddStringToObject(pRoot, "replyType", "DEVICE_FILE_UPLOAD_REQ");
	cJSON_AddStringToObject(pRoot, "clientId", RouteID);
	cJSON_AddStringToObject(pRoot, "tagClientId", tagid);
	cJSON_AddStringToObject(pRoot, "content", cJSON_Print(pRow));
	printf("### pRoot,%s ### \n", cJSON_Print(pRoot));
	char *buffer = cJSON_Print(pRoot);
	buffer[strlen(buffer)] = '\0';

	if (pRow != NULL)
		cJSON_Delete(pRow);

	if (pRoot != NULL)
		cJSON_Delete(pRoot);

	ret = send_msg(sockfd, buffer, strlen(buffer));
	free(buffer);
	buffer = NULL;

	return ret;
}

/* 
--------------------------------------------------------------------------------
	函数功能：字符串转换为十六进制
--------------------------------------------------------------------------------
*/
int CharToHex(const char *src, unsigned char *dst, int len)
{
	const char *HexList = "0123456789ABCDEF";
	unsigned int i = 0, j = 0, index = 0;

	for (i = 0, j = 0; i < len; i++, j += 2)
	{
		index = (src[i] & 0xf);
		dst[i * 2 + 1] = HexList[index];
		index = ((src[i] >> 4) & 0xf);
		dst[i * 2] = HexList[index];
	}

	return j;
}

/* 
--------------------------------------------------------------------------------
	函数功能：读取二进制文件
--------------------------------------------------------------------------------
*/
int read_bytes(int sockfd, char *tagid, int totalSize, char *fileId, char *filePath)
{
	int ret = 0, len = 0, size = 0, index = 0;
	FILE *fp;
	char fileName[128];
	char data[2 * MaxSize + 1024];
	char buff[MaxSize];
	cJSON *pRow = NULL, *pRoot = NULL;

	if ((fp = fopen(filePath, "rb")) == NULL)
	{
		printf("open file %s error, fp == NULL !\n", filePath);
		return -1;
	}

	memset(fileName, 0, sizeof(fileName));
	memcpy(fileName, filePath, sizeof(fileName));

	memset(buff, 0, sizeof(buff));
	while ((size = fread(buff, sizeof(unsigned char), MaxSize, fp)) != 0)
	{
		/*		int i=0;
		for(i=0;i<size;i++)
			printf("%02hhx ", buff[i]);
		printf("\n");
*/
		memset(data, 0, sizeof(data));
		len = CharToHex(buff, data, size);

		pRow = cJSON_CreateObject();
		cJSON_AddStringToObject(pRow, "fileId", fileId);
		cJSON_AddStringToObject(pRow, "fileName", fileName);
		cJSON_AddStringToObject(pRow, "filePath", filePath);
		cJSON_AddNumberToObject(pRow, "totalSize", totalSize);
		cJSON_AddStringToObject(pRow, "sizeUnit", "");
		cJSON_AddNumberToObject(pRow, "currentIndex", index);
		cJSON_AddNumberToObject(pRow, "size", len);
		cJSON_AddStringToObject(pRow, "byte", data);

		pRoot = cJSON_CreateObject();
		cJSON_AddStringToObject(pRoot, "type", "DEVICE_FILE_DOWNLOAD_RSP");
		cJSON_AddStringToObject(pRoot, "replyType", "DEVICE_FILE_DOWNLOAD_REQ");
		cJSON_AddStringToObject(pRoot, "clientId", RouteID);
		cJSON_AddStringToObject(pRoot, "tagClientId", tagid);
		cJSON_AddStringToObject(pRoot, "content", cJSON_Print(pRow));
		printf("### pRoot,%s ### \n", cJSON_Print(pRoot));
		char *buffer = cJSON_Print(pRoot);
		buffer[strlen(buffer)] = '\0';

		if (pRow != NULL)
			cJSON_Delete(pRow);

		if (pRoot != NULL)
			cJSON_Delete(pRoot);

		ret = send_msg(sockfd, buffer, strlen(buffer));
		free(buffer);
		buffer = NULL;

		memset(buff, 0, sizeof(buff));
		index += size;
	}

	fclose(fp);

	return 0;
}

/* 
--------------------------------------------------------------------------------
	函数功能：文件下载,路由器读取文件上传
--------------------------------------------------------------------------------
*/
int disk_read_file(int sockfd, char *tagid, char *msg)
{
	int ret = 0, send = 1;
	char fileId[128];
	char filePath[256];
	char data[1024];
	cJSON *pRow = NULL, *pRoot = NULL, *pValue = NULL;

	pRow = cJSON_CreateObject();

	pRoot = cJSON_Parse(msg);
	if (pRoot == NULL)
	{
		cJSON_AddNumberToObject(pRow, "code", 1);
		cJSON_AddStringToObject(pRow, "msg", "JSON解析失败!");
	}
	else
	{
		//文件ID
		memset(fileId, 0, sizeof(fileId));
		pValue = cJSON_GetObjectItem(pRoot, "fileId");
		if (pValue != NULL && pValue->type == cJSON_String)
			memcpy(fileId, pValue->valuestring, sizeof(fileId));

		//文件名称
		memset(filePath, 0, sizeof(filePath));
		pValue = cJSON_GetObjectItem(pRoot, "fileName");
		if (pValue != NULL && pValue->type == cJSON_String)
			memcpy(filePath, pValue->valuestring, sizeof(filePath));

		struct stat file;
		if (stat(filePath, &file) < 0)
		{
			cJSON_AddNumberToObject(pRow, "code", 1);
			cJSON_AddStringToObject(pRow, "msg", "文件不存在");
		}
		else
		{
			send = 0;
			read_bytes(sockfd, tagid, file.st_size, fileId, filePath);
		}
	}

	pRoot = cJSON_CreateObject();
	cJSON_AddStringToObject(pRoot, "type", "COMMON_SUCCESS");
	cJSON_AddStringToObject(pRoot, "replyType", "DEVICE_FILE_DOWNLOAD_REQ");
	cJSON_AddStringToObject(pRoot, "clientId", RouteID);
	cJSON_AddStringToObject(pRoot, "tagClientId", tagid);
	cJSON_AddStringToObject(pRoot, "content", cJSON_Print(pRow));

	char *buffer = cJSON_Print(pRoot);
	buffer[strlen(buffer)] = '\0';

	if (pRow != NULL)
		cJSON_Delete(pRow);

	if (pRoot != NULL)
		cJSON_Delete(pRoot);

	if (send)
	{
		ret = send_msg(sockfd, buffer, strlen(buffer));
	}
	free(buffer);
	buffer = NULL;

	return ret;
}